<template>
    <div class="fullScreen bg-c-g" ref="myRecord">
        <!-- header -->
        <m-header :msg="'战绩'"></m-header>
        <!-- tab -->
        <tab-temp :tabArr="['全部', '中奖', '待开奖', '未成功']"></tab-temp>
        <!-- header结束 -->
        <mescroll-vue v-show="$store.state.tabTogIndex == 0" ref="mescroll0" :up="mescrollUp0" @init="mescrollInit0">
            <div class="tx-c-222">
                <no-data v-if="status[0]"></no-data>
                <my-record :dataList="list[0]"></my-record>
            </div>
        </mescroll-vue>
        <mescroll-vue v-show="$store.state.tabTogIndex == 1" ref="mescroll1" :up="mescrollUp1" @init="mescrollInit1">
            <div class="tx-c-222">
                <no-data v-if="status[1]"></no-data>
                <my-record :dataList="list[1]"></my-record>
            </div>
        </mescroll-vue>
        <mescroll-vue v-show="$store.state.tabTogIndex == 2" ref="mescroll2" :up="mescrollUp2" @init="mescrollInit2">
            <div class="tx-c-222">
                <no-data v-if="status[2]"></no-data>
                <my-record :dataList="list[2]"></my-record>
            </div>
        </mescroll-vue>
        <mescroll-vue v-show="$store.state.tabTogIndex == 3" ref="mescroll3" :up="mescrollUp3" @init="mescrollInit3">
            <div class="tx-c-222">
                <no-data v-if="status[3]"></no-data>
                <my-record :dataList="list[3]"></my-record>
            </div>
        </mescroll-vue>
    </div>
</template>
<script type="text/ecmascript-6">
import MHeader from '../../components/m-header'
import TabTemp from '../../components/tabTemp'
import MescrollVue from 'mescroll.js/mescroll.vue'
import MyRecord from '../../components/myRecordTemp'
import NoData from '../../components/noData'
import { isEmptyObject } from '../../common/js/common'
import { tickets } from '../../common/js/axiosUtil' 
export default {
    data() {
        return {
            type: [
                'all', 'yzj', 'dkj', 'wcg'
            ],
            list: [
                [], [], [], []
            ],
            status: [
                false, false, false, false
            ],
            mescrollArr: new Array(3),// mescroll实例对象
            mescrollUp0: { // 上拉加载的配置.
                callback: this.upCallback0, // 上拉回调
                htmlNodata: '<p class="upwarp-nodata">没有更多数据了</p>',
                noMoreSize: 5 
            },
            mescrollUp1: { // 上拉加载的配置.
                callback: this.upCallback1, // 上拉回调
                htmlNodata: '<p class="upwarp-nodata">没有更多数据了</p>',
                noMoreSize: 5 
            },
            mescrollUp2: { // 上拉加载的配置.
                callback: this.upCallback2, // 上拉回调
                htmlNodata: '<p class="upwarp-nodata">没有更多数据了</p>',
                noMoreSize: 5 
            },
            mescrollUp3: { // 上拉加载的配置.
                callback: this.upCallback3, // 上拉回调
                htmlNodata: '<p class="upwarp-nodata">没有更多数据了</p>',
                noMoreSize: 5 
            }
        }
    },
    created() {
        this.getTickets(0, 1, 10)
        this.getTickets(1, 1, 10)
        this.getTickets(2, 1, 10)
        this.getTickets(3, 1, 10)
    }, 
    methods: {
        getTickets (type, page, count) { 
            let params = new URLSearchParams()
            const token = this.$token()
            params.append('timeStamp', token[0])
            params.append('token', token[1])
            params.append('uid', this.$store.state.bfUserInfo.uid)
            params.append('type', this.type[type])
            params.append('page', page)
            params.append('count', count)
            tickets(params)
            .then((response)=> {
                if(response.data.code == "200"){
                    const {data} = response.data
                    if(isEmptyObject(data)){
                        if(page === 1){
                            // 没有数据时显示提示界面,隐藏下拉刷新和上拉加载的状态;
                            this.mescrollArr[type].endErr()
                            this.status[type] = true 
                        }else{
                            // 上一页数据为10条本页数据为0时,显示提示信息并禁止上拉事件
                            this.$nextTick(() => {
                                this.mescrollArr[type].endSuccess(data.length)
                            })
                        }
                    }else{
                        // 如果是第一页需手动制空列表
                        if (page === 1) {
                            this.list[type] = []
                        }
                        // 把请求到的数据添加到列表
                        this.list[type] = this.list[type].concat(data) 
                        // 数据渲染成功后,隐藏下拉刷新的状态
                        this.$nextTick(() => {
                            this.mescrollArr[type].endSuccess(data.length)
                        })
                    }
                    // 数据层次太多，没有触发render函数进行自动更新，需手动调用
                    this.$forceUpdate()
                } else {
                    this.mescrollArr[type].endErr()
                    this.$logAgain(response.data.msg)
                }
            })
            .catch((error)=> {
                // 联网失败的回调,隐藏下拉刷新和上拉加载的状态;
                this.mescrollArr[type].endErr()
                this.$toast('请求服务失败', 2000)
            })
        },
        // mescroll组件初始化的回调,可获取到mescroll对象
        mescrollInit0 (mescroll) {
            this.mescrollArr[0] = mescroll
        },
        mescrollInit1 (mescroll) {
            this.mescrollArr[1] = mescroll
        },
        mescrollInit2 (mescroll) {
            this.mescrollArr[2] = mescroll
        },
        mescrollInit3 (mescroll) {
            this.mescrollArr[3] = mescroll
        },
        // 上拉回调 page = {num:1, size:10}
        upCallback0 (page) {
            this.getTickets(0, page.num, page.size)
        },
        upCallback1 (page) {
            this.getTickets(1, page.num, page.size)
        },
        upCallback2 (page) {
            this.getTickets(2, page.num, page.size)
        },
        upCallback3 (page) {
            this.getTickets(3, page.num, page.size)
        }
    },
    components: { 
        MHeader,
        TabTemp,
        MescrollVue,
        MyRecord,
        NoData
    }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
    /*通过fixed固定mescroll的高度*/
    .mescroll
        position: fixed
        top: 5.75rem
        bottom: 0
        height: auto
        background: white
</style>